<?php
/* 
 * Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace org\phpstruct\json;

/**
 * Description of JsonAPI
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 */
class JsonResponse {

	public static function handleJsonSuccess($json_active,$msg="",$data=array()) {

		if($json_active) {
			$js = new JsonSuccess($msg,$data);
		} else {
			return true;
		}

	}//:~

	public static function handleJsonCustomSuccess($json_active,$msg="",$data=array()) {

		if($json_active) {
			$js = new JsonCustomSuccess($msg,"JSONCustomSuccess",$data);
		} else {
			return true;
		}

	}//:~


	public static function handleJsonResponse($json_active,$data) {

		if($data instanceof Rebel_DB_ORM_Pager) {
			if($data->getResults() && $data->results instanceof stdClass)  {
				$data->results = array($data->results);
			}
		}

		if($json_active) {
			die(json_encode($data));
		} else {
			return $data;
		}
	}//:~

	public static function handleJsonException($json_active,$e) {

		if($json_active) {
			throw new JsonException($e);
		} else {
			Logger::log($e,Logger::TYPE_FATAL);
			throw $e;
		}
	}//:~


}

